#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include "constantes.h"
#include "structures.h"
#include "explorer.h"
#include "tools.h"
#include "debug.h"

/* #include <sys/types.h> */
/* #include <sys/stat.h> */

/*
 * Draw
 * Browse
 * Open
 */


/*
 * draw the content of a directory
 *
 * 4each file : give an id [0 ->| 9] => getch
 * if id == folder : Ctrl + number => open all in folder
 * if id == folder : Ctrl + Shift + Alt : Open ALL in folder (recursively)
 *
 * '+' => developp directory [tree like]
 *
 * full_block : folder
 * empty rectangle : std_file
 * int : readable => check magic number
 *	=> getch == int_file_readable ==> play
 */

/* disp properties in infobar */


char			*choose_symb(unsigned char		type)
{
  switch (type)
  {
  case DT_DIR: /* directory */
    return ("O");

  case DT_REG: /* regular file */
    return ("~");

  case DT_LNK: /* symbolic link */
  default:
    return ("?");
  }
}

int			explorer_list(p_context			c)
{ /* param : char *symbols*/
  DIR			*folder = opendir(".");
  struct dirent		*file;
  int			line = 0;
  int			i = 0;
  char			*symb; /* grepme replace by param */

  if (!folder)
  {
    mvwprintw(WEXPLORER, 1, 3, "Invalid DIR");
    return (-1);
  }

  while ((file = readdir(folder)))
  {
    if (strcmp(".", file->d_name) == 0)
      continue;

    switch (file->d_type)
    {
    case DT_DIR: /* directory */
      symb = "O";
      break;

    case DT_REG: /* regular file */
      symb = "~";
      break;

    case DT_LNK: /* symbolic link */
    default:
      symb = "?";
      break;
    }

    /* if (get_font_state(c) == 0) /\* grepme tmp sol (bad mouse support) *\/ */
    /* { */
    /*   mvwprintw(WEXPLORER, line, 1, symb/\* SYMB_FOLDER *\/); */
    /*   mvwprintw(WEXPLORER, line, 2, " %i- ", i); */
    /*   mvwprintw(WEXPLORER, line, 7, file->d_name); */
    /* } */

    SWEXPLORER[i] = derwin(WEXPLORER,
			   (2 + FONT_SIZE), WIDTH_EXPLORER - 2 * IS_FRAME,
			   line * (FONT_SIZE + 2) + IS_FRAME, IS_FRAME);

    EXPLORER_FILENAME[i] = NULL;
    EXPLORER_FILENAME[i] = file->d_name;

    line ++;
    i++;
  }
  closedir(folder);

  display_frame(WEXPLORER);
  display_name(c, ZONE_EXPLORER);

  c->nb_display_files = i;
  explorer_display_filename(c);

  touchwin(WEXPLORER);
  wrefresh(WEXPLORER);

  return (0);
}




void			explorer_display_filename(p_context	c)
{
  WINDOW		*w;
  int			i, j, line, font_level;
  int			nb_char = 0;
  char			*str;


  font_level = get_font_state(c);

  if (font_level == 0)
  {
    for (i = 0; i < c->nb_display_files; i++)
    {
      mvwaddstr(SWEXPLORER[i], IS_FRAME, IS_FRAME, EXPLORER_FILENAME[i]);
      if (IS_FRAME)
	display_frame(SWEXPLORER[i]);
    }

    return;
  }

  for (i = 0; i < c->nb_display_files; i++)
  {
    str = EXPLORER_FILENAME[i];
    for (j = 0; str[j]; str++)
    {
      if (str[j] == '\n')
      {
	line += FONT_SIZE;
	nb_char = 0;
	continue;
      }

      if (str[j] == ' ')
      {
	nb_char ++;
	continue;
      }

      /* if (nb_char * size_char > WIDTH[zone]) */
      /*   /\* grepme print ... *\/ */
      /*   return; */

      w = derwin(SWEXPLORER[i], FONT_SIZE, FONT_SIZE, 0, nb_char * FONT_SIZE);

      debug_display_frame(w);
      mvwaddstr(w, 0, 0, char2aa(str[j], font_level));

      delwin(w);
      nb_char ++;
    }
  }
}



char			*explorer_is_in(p_context		c)
{
  int			i;

  for (i = 0; i < c->nb_display_files; i++)
    if (wenclose(SWEXPLORER[i], c->mouse.y, c->mouse.x))
    /* { */
    /*   mvwprintw(stdscr, 0, 0, "FILENAME : %s", EXPLORER_FILENAME[i]); */
    /*   debug_pause(); */
      return (EXPLORER_FILENAME[i]);
    /* } */

  return (NULL);
}
